package com.spring.business.configure.controller;

import com.spring.business.configure.entity.SysConf;
import com.spring.business.configure.pojo.dto.DictListDTO;
import com.spring.business.configure.pojo.dto.SysConfDTO;
import com.spring.business.configure.pojo.vo.DictTypeVO;
import com.spring.business.configure.pojo.vo.SysConfVO;
import com.spring.business.configure.service.IConfigureService;
import com.spring.starter.resolver.BeanResolver;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 提供配置相关的能力
 * 数据字典
 */
@RestController
@Api("配置管理")
@RequestMapping("/configure")
public class ConfigureController
{
	@Autowired
	private IConfigureService configureService;
	@Autowired
	private BeanResolver beanResolver;

	@ApiOperation("根据指定数据字典类型获取数据字典值")
	@PostMapping("/dict/type/{code}")
	public DictTypeVO getDitType(@PathVariable String code)
	{
		return configureService.getDitType(code);
	}

	@ApiOperation("根据字典类型列表获取多数据字典")
	@PostMapping("/dict/type/list")
	public List<DictTypeVO> getDitTypeList(@RequestBody DictListDTO dictListDTO)
	{
		return configureService.getDitTypeList(dictListDTO);
	}

	@ApiOperation("获取租户下系统参数")
	@PostMapping("/sys/conf/tenant/{tenant}")
	public List<SysConfVO> getTenantSysConf(@PathVariable String tenant)
	{
		List<SysConf> sysConfList = configureService.getTenantSysConf(tenant);
		List<SysConfVO> sysConfVOList = new ArrayList<>(sysConfList.size());
		sysConfList.forEach(sysConf -> {
			sysConfVOList.add(beanResolver.map(sysConf, SysConfVO.class));
		});
		return sysConfVOList;
	}

	@ApiOperation("根据指定配置类型获取配置数据")
	@PostMapping("/sys/conf/{code}")
	public SysConfVO getSysConf(@PathVariable String code)
	{
		return configureService.getSysConf(code);
	}

	@ApiOperation("根据指定配置类型模糊匹配配置数据")
	@PostMapping(value = "/sys/conf/like/{code}")
	public List<SysConf> getSysConfByCodeLike(@PathVariable("code") String code)
	{

		return configureService.getSysConfLike(code);
	}

	@ApiOperation("修改租户系统参数")
	@PostMapping(value = "/sys/conf/tenant/update")
	public void updateTenantSysConf(@RequestBody SysConfDTO sysConfDTO)
	{

		configureService.updateTenantSysConf(sysConfDTO);
	}

	@ApiOperation("获取UserID")
	@GetMapping("/userid/init")
	public Integer initUserId()
	{
		return configureService.getUserID();
	}


	@ApiOperation("根据模块编码获取业务编码。格式：模块编码 + 年月日时分秒 + 序列号")
	@GetMapping("/businesscode/init")
	public String initBusinessCode(String module)
	{
		return configureService.getSerialCode(module);
	}


}
